<?php

class Product_model extends Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::Model();

		// Load needed models, libraries, helpers and language files
		//$this->load->module_model('blog', 'category_model', 'category');
		
		$this->_table = $this->config->item('database_tables');
	}

	// Public methods
	public function filterprice($from,$to)
	{
		$this->db->select('products.*');
		$this->db->from('products');
		if($to == 0)
		{
			$this->db->where('price >',$from);
		}
		else
		{
			$this->db->where('price >',$from);
			$this->db->where('price <=',$to);
		}
		$this->db->order_by('id', 'DESC');
			
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}
	public function get_the_last_sticky_post($lang_id=2)
	{
		$this->db->select_max('news.id','ID');
		$this->db->from($this->_table['news'] . ' news');
		$this->db->where('news.status', '1');
		$this->db->where('news.lang_id',$lang_id);
		$this->db->where('news.type','n');	
		$this->db->where('news.sticky','1');	
			
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$last_id = $query->result_array();
		}
		
		$result = $this->get_post_by_id($last_id[0]['ID']);
		return $result;
	}
	public function get_page_by_url($url_title)
	{
		$this->db->select('news.*, user.display_name');
		$this->db->from($this->_table['news'] . ' news');
		$this->db->join($this->_table['user'] . ' user', 'news.creator_id = user.id');
		$this->db->where('news.status', '1');
		$this->db->where('news.url_title', $url_title);
		$this->db->where('news.type', 'p');
		$this->db->limit(1);	
		$query = $this->db->get();
		
		if ($query->num_rows() == 1)
		{
			$result = $query->row_array();
			
			//$result['categories'] = $this->categories->get_categories_by_ids($this->get_post_categories($result['id']));
			//$result['comment_count'] = $this->db->where('post_id', $result['id'])->from($this->_table['comments'])->count_all_results();
			//print_r( $result);
			return $result;
		}
	}
	public function get_posts_per_page()
	{
		$this->db->select('value');
		$this->db->where('name', 'posts_per_page');
			
		$query = $this->db->get($this->_table['option'], 1);
			
		if ($query->num_rows() == 1)
		{
			$row = $query->row_array();
		}
		else
		{
			return 5;
		}
			
		return $row['value'];
	}

	public function get_posts_count($lang_id = 2)
	{
		$this->db->select('id');
		$this->db->where('published', '1');
		$this->db->where('lang_id',$lang_id);
		$query = $this->db->count_all_results($this->_table['products']);			
		return $query;
	}
	
	public function get_post_categories($post_id)
	{
		return null;
		$this->db->select('category_id');
		$this->db->where('post_id', $post_id);
		
		$query = $this->db->get($this->_table['posts_to_categories']);
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			
			foreach ($result as $category)
			{
				$categories[] = $category['category_id'];
			}
			
			return $categories;
		}
	}

	public function get_posts($number = 10, $offset = 0,$lang_id = 2)
	{
		$current_date = date('Y-m-d');
		
		$this->db->select('products.*, s_category.title as category');
		$this->db->from($this->_table['products'] . ' products');
		$this->db->join($this->_table['s_category'] . ' s_category', 'products.category_id = s_category.id');
		$this->db->where('products.published', '1');
		$this->db->where('products.lang_id',$lang_id);
		$this->db->order_by('id', 'DESC');
		$this->db->limit($number, $offset);
			
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			
			foreach (array_keys($result) as $key)
			{
				//$result[$key]['categories'] = $this->categories->get_categories_by_ids($this->get_post_categories($result[$key]['id']));
				//$result[$key]['comment_count'] = $this->db->where('post_id', $result[$key]['id'])->from($this->_table['comments'])->count_all_results();
			}
			return $result;
		}
	}
	public function get_relate_product($id)
	{
		// Get the product by ID
		$obj = $this->get_product($id);
		$category_id = $obj['category_id'];
		return $this->get_product_by_category($category_id,5);
	}
	public function get_product_by_category($id,$limit=0)
	{
		
		$this->db->select('products.*');
		$this->db->from($this->_table['products'] . ' products');
		$this->db->join($this->_table['s_category'] . ' s_category', 'products.category_id = s_category.id');
		$this->db->where('products.published', 1);
		$this->db->where('products.category_id', $id);
		if($limit!=0)
		{
			$this->db->limit($limit);
		}
		$this->db->order_by('products.id', 'DESC');
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			
			foreach (array_keys($result) as $key)
			{
				//$result[$key]['categories'] = $this->categories->get_categories_by_ids($this->get_post_categories($result[$key]['id']));
				//$result[$key]['comment_count'] = $this->db->where('post_id', $result[$key]['id'])->from($this->_table['comments'])->count_all_results();
			}

			return $result;
		}
	}

	public function get_post_by_url($year, $month, $day, $url_title)
	{
		$date = $year . '-' . $month . '-' . $day;
		
		$this->db->select('news.*, user.display_name');
		$this->db->from($this->_table['news'] . ' news');
		$this->db->join($this->_table['user'] . ' user', 'news.creator_id = user.id');
		$this->db->where('news.status', '1');
		$this->db->where('news.url_title', $url_title);
		$this->db->where('news.created_date', $date);
		$this->db->limit(1);	
		$query = $this->db->get();
		
		if ($query->num_rows() == 1)
		{
			$result = $query->row_array();
			
			//$result['categories'] = $this->categories->get_categories_by_ids($this->get_post_categories($result['id']));
			//$result['comment_count'] = $this->db->where('post_id', $result['id'])->from($this->_table['comments'])->count_all_results();
			//print_r( $result);
			return $result;
		}
	}
	public function get_the_last_post($lang_id=2)
	{
		$this->db->select_max('news.id','ID');
		$this->db->from($this->_table['news'] . ' news');
		$this->db->where('news.status', '1');
		$this->db->where('news.lang_id',$lang_id);
		$this->db->where('news.type','n');		
			
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$last_id = $query->result_array();
		}
		
		$result = $this->get_post_by_id($last_id[0]['ID']);
		return $result;
	}
	public function get_product($id)
	{
		$this->db->select('products.*');
		$this->db->from($this->_table['products'] . ' products');
		$this->db->where('products.published', 1);
		$this->db->where('products.id', $id);
		$this->db->limit(1);
		
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result[0];
		}
		return null;
	}
	
	public function get_posts_by_tags($tag_name)
	{
		$current_date = date('Y-m-d');
		
		$this->db->select('posts.id, posts.author, posts.date_posted, posts.title, posts.url_title, posts.excerpt, posts.content, posts.allow_comments, posts.sticky, posts.status, posts.author, users.display_name');
		$this->db->from($this->_table['posts'] . ' posts');
		$this->db->join($this->_table['users'] . ' users', 'posts.author = users.id');
		$this->db->join($this->_table['tags_to_posts'] . ' tags_to_posts', 'posts.id = tags_to_posts.post_id');
		$this->db->join($this->_table['tags'] . ' tags', 'tags_to_posts.tag_id = tags.id');
		$this->db->where('posts.status', 'published');
		$this->db->where('posts.date_posted <=', $current_date);
		$this->db->where('tags.name', $tag_name);
		$this->db->order_by('posts.sticky', 'DESC');
		$this->db->order_by('posts.id', 'DESC');
			
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			
			foreach (array_keys($result) as $key)
			{
				$result[$key]['categories'] = $this->categories->get_categories_by_ids($this->get_post_categories($result[$key]['id']));
				$result[$key]['comment_count'] = $this->db->where('post_id', $result[$key]['id'])->from($this->_table['comments'])->count_all_results();
			}

			return $result;
		}
	}

	public function search_by_term($term)
	{
		$this->db->select('products.*');
		$this->db->from('products');
		$this->db->like('code', $term);
		$this->db->orlike('title', $term);
		$this->db->orlike('description', $term);
		$this->db->order_by('id', 'DESC');
			
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}
}

/* End of file blog_model.php */
/* Location: ./application/modules/blog/models/blog_model.php */